package com.kid.keep.controller.impl;

import com.kid.keep.controller.IUserController;
import com.kid.keep.dto.AddUserDTO;
import com.kid.keep.dto.ChildDTO;
import com.kid.keep.dto.UserDTO;
import com.kid.keep.mapper.IUserCovertMapper;
import com.kid.keep.request.AddUserRequest;
import com.kid.keep.service.IChildService;
import com.kid.keep.service.IUserService;
import com.kid.keep.vo.AddUserVO;
import com.kid.keep.vo.UserVO;
import com.honeybee.cloud.framework.common.response.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 用户相关请求
 * @Author JOHN
 * @Create 2020-01-02
 * @Since 1.0.0
 */
@Slf4j
@RestController
public class UserController implements IUserController {

    private IUserService userService;


    private IUserCovertMapper userCovertMapper;
    private IChildService childService;


    @Autowired
    public void setUserCovertMapper(IUserCovertMapper userCovertMapper) {
        this.userCovertMapper = userCovertMapper;
    }

    @Autowired
    public UserController(IUserService userService,IChildService childService) {
        this.userService = userService;
        this.childService = childService;
    }

    @Override
    public ResponseEntity<AddUserVO> addUser(AddUserRequest addUserRequest) {
        AddUserDTO addUserDTO = userCovertMapper.toDTO(addUserRequest);
        UserDTO userDTO = userService.addUser(addUserDTO);
        AddUserVO addUserVO = userCovertMapper.toAddUserVO(userDTO);
        return ResponseEntity.ok(addUserVO);
    }

    @Override
    public ResponseEntity<UserVO> getUserById(Long userId) {
        UserDTO userDTO = userService.findById(userId);
        UserVO userVO = userCovertMapper.toVO(userDTO);
        return ResponseEntity.ok(userVO);
    }
    @Override
    public ResponseEntity<Integer> countChildByParentId(Long userId) {
        List<ChildDTO> childDTOList = childService.listChildByParentId(userId);
        return ResponseEntity.ok(childDTOList.size());
    }
}
